package dao;
import tool.OtherToolClass;
import tool.RowMapper;
import tool.BaseDao;
import java.util.List;
import java.sql.SQLException;
import java.sql.ResultSet;
import entity.SizetbEntity;
import dao.mapper.SizetbDAOI;

/**
 *@author JiangFan
 */
public class SizetbDAO extends BaseDao implements SizetbDAOI {

	OtherToolClass ot=new OtherToolClass ();
	/**
	 * @param size_name
	 *         根据尺寸名查询尺寸列表是否存在该尺寸
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	@Override
	public SizetbEntity selectSizeOne (String size_name) throws SQLException, ClassNotFoundException {
		String sql="select *from sizetb where size_name=?";
		Object[] param={size_name};
		//调用查询单个方法
		return this.excuteQueryOne (sql, param,new SizetbRowMapper () );
	}

	/**
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	@Override
	public List <SizetbEntity> selectSizeAll ()
			throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
		String sql="select *from sizetb";
		//调用查询方法
		return this.executeQuery (sql,null,new SizetbRowMapper ());
	}

	/**
	 * @param se
	 *         在尺寸列表添加尺寸值
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	@Override
	public int insertSize (SizetbEntity se) throws SQLException, ClassNotFoundException {
		String sql="insert into sizetb values(?,?)";
		//调用添加方法
		Object[] param={getID (),se.getSize_name ()};
		return this.update (sql,param);
	}
	private String getID() throws SQLException, ClassNotFoundException {
		String sql="select size_id from sizetb";
		return ot.getIDtwo (sql,"siz",new SizetbRowMapper ());
	}
}
class SizetbRowMapper implements RowMapper<SizetbEntity>{
	@Override
	public SizetbEntity rowmap(ResultSet set) throws  SQLException{
		return new SizetbEntity(
				set.getString("size_id"),
				set.getString("size_name")
		);
	}
}
